package com.ly.lypopupwindowdemo.activity;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;

public class DongHuaDemoActivity extends AppCompatActivity implements View.OnClickListener {

    Button btn1, btn2, btn3, btn4;
    ImageView img1, img2, img3, img4;
    AnimationDrawable animation1, animation2, animation3, animation4;

    Button btn5, btn6;
    ImageView img5, img6, img7;
    Animation anim1;

    TextView tv_tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dong_hua_demo);

        findViews();
        setListeners();

    }

    private void setListeners() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);

        findViewById(R.id.btn7).setOnClickListener(this);
        findViewById(R.id.first_day).setOnClickListener(this);

    }

    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        tv_tip = (TextView) findViewById(R.id.tv_tip);

        img1 = (ImageView) findViewById(R.id.img1);
        img2 = (ImageView) findViewById(R.id.img2);
        img3 = (ImageView) findViewById(R.id.img3);
        img4 = (ImageView) findViewById(R.id.img4);
        img5 = (ImageView) findViewById(R.id.img5);
        img6 = (ImageView) findViewById(R.id.img6);
        img7 = (ImageView) findViewById(R.id.img7);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                //帧动画
            {
                int isShow = img1.getVisibility();
                if (isShow == img1.VISIBLE) {
                    img1.setVisibility(View.GONE);
                } else {
                    img1.setVisibility(View.VISIBLE);
                    img1.setImageResource(R.drawable.frame_anim1);
                    animation1 = (AnimationDrawable) img1.getDrawable();
                    animation1.start();
                }
            }
            break;
            case R.id.btn2:
                //帧动画
            {
                int isShow = img2.getVisibility();
                if (isShow == img2.VISIBLE) {
                    img2.setVisibility(View.GONE);
                } else {

                    img2.setVisibility(View.VISIBLE);
                    img2.setImageResource(R.drawable.frame_anim2);
                    animation2 = (AnimationDrawable) img2.getDrawable();
                    animation2.start();
                }
            }
            break;
            case R.id.btn3:
                //帧动画
            {
                int isShow = img3.getVisibility();
                if (isShow == img3.VISIBLE) {
                    img3.setVisibility(View.GONE);
                } else {
                    img3.setVisibility(View.VISIBLE);
                    img3.setImageResource(R.drawable.frame_anim3);
                    animation3 = (AnimationDrawable) img3.getDrawable();
                    animation3.start();
                }
            }
            break;
            case R.id.btn4: {
                int isShow = img4.getVisibility();
                if (isShow == img4.VISIBLE) {
                    img4.setVisibility(View.GONE);
                } else {
                    img4.setVisibility(View.VISIBLE);
                    img4.setImageResource(R.drawable.frame_anim4);
                    animation4 = (AnimationDrawable) img4.getDrawable();
                    animation4.start();
                }
            }
            break;

            case R.id.btn5:
                //补间动画
//                int isShow = img5.getVisibility();
//                if (isShow == img5.VISIBLE) {
//                    img5.setVisibility(View.GONE);
//                } else {
//                    img5.setVisibility(View.VISIBLE);
//                    anim1 = AnimationUtils.loadAnimation(this,R.anim.fade_in);
//                    img5.startAnimation(anim1);
//
//                }

//                img5.setVisibility(View.VISIBLE);
//                anim1 = AnimationUtils.loadAnimation(this,R.anim.alpha_anim);
//                //决定了动画播放结束时是否保持最终的状态
//                anim1.setDuration(1000);
//                anim1.setRepeatMode(Animation.RESTART);
//                anim1.setRepeatCount(10);
//                img5.startAnimation(anim1);


                img5.setVisibility(View.VISIBLE);
                anim1 = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
                //决定了动画播放结束时是否保持最终的状态
                img5.startAnimation(anim1);
                break;

            case R.id.btn6:
                //属性动画

                // RotateAnimation();


                //  AlpahAnimation();

                ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(img6, "alpha", 1.0f, 0.5f, 0.8f, 1.0f);
                ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(img6, "scaleX", 0.0f, 1.0f);
                ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(img6, "scaleY", 0.0f, 2.0f);
                ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(img6, "rotation", 0, 360);
//                ObjectAnimator transXAnim = ObjectAnimator.ofFloat(img6, "translationX", 100, 400);
//                ObjectAnimator transYAnim = ObjectAnimator.ofFloat(img6, "tranlsationY", 100, 750);
                AnimatorSet set = new AnimatorSet();
                //set.playTogether(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
                // set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim);
                set.setDuration(3000);
                set.start();
                break;

            case R.id.btn7:
                //属性动画

                //valueAnimator();
                //AlphaAnimtor();

                //RotationAnimator();

                // ScaleAnimator();

                TranslateAnimator();

                ZuHeDongHua();

                //xmlDongHua();

                break;
            case R.id.first_day:
                //国庆归来第一天动画练习
                startActivity(new Intent(this,DHLianxiActivity.class));
                break;
            default:
                break;
        }

    }

    private void xmlDongHua() {

        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.animatorset1);
        animator.setTarget(img7);
        animator.start();
    }

    private void ZuHeDongHua() {

        ObjectAnimator moveIn = ObjectAnimator.ofFloat(img7, "translationX", -500f, 0f);
        ObjectAnimator rotate = ObjectAnimator.ofFloat(img7, "rotation", 0f, 360f);
        ObjectAnimator fadeInout = ObjectAnimator.ofFloat(img7, "alpha", 1f, 0f, 1f);

        AnimatorSet animSet = new AnimatorSet();
        /**
         * fadeInout和rotate动画一起执行并且这个动画要在moveIn之后执行
         *after(Animator anim)   将现有动画插入到传入的动画之后执行
         after(long delay)   将现有动画延迟指定毫秒后执行
         before(Animator anim)   将现有动画插入到传入的动画之前执行
         with(Animator anim)   将现有动画和传入的动画同时执行
         */
        animSet.play(fadeInout).with(rotate).after(moveIn);
        animSet.setDuration(2000);
        animSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Toast.makeText(DongHuaDemoActivity.this, "动画结束监听", Toast.LENGTH_SHORT).show();
            }
        });
//        animSet.addListener(new Animator.AnimatorListener() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationCancel(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationRepeat(Animator animation) {
//
//            }
//        });
        animSet.start();


    }

    private void ScaleAnimator() {

        ObjectAnimator animator = ObjectAnimator.ofFloat(img7, "scaleY", 1f, 3f, 1f);
        animator.setDuration(5000);
        animator.start();
    }

    private void TranslateAnimator() {
        /**
         * 获取tv_tip当前位置
         */
        float curTranslationX = tv_tip.getTranslationX();

        ObjectAnimator animator = ObjectAnimator.ofFloat(tv_tip, "translationX", curTranslationX, -20f, curTranslationX + 20f, curTranslationX);
        animator.setDuration(100);
        animator.setRepeatCount(2);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.start();
    }

    private void RotationAnimator() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(img7, "rotation", 0f, 360f);
        animator.setDuration(5000);
        animator.start();

    }

    private void AlphaAnimtor() {

        /**
         * 我们还是调用了ofFloat()方法来去创建一个ObjectAnimator的实例，
         * 只不过ofFloat()方法当中接收的参数有点变化了。
         * 这里第一个参数要求传入一个object对象，我们想要对哪个对象进行动画操作就传入什么，
         * 这里我传入了一个textview。第二个参数是想要对该对象的哪个属性进行动画操作，
         * 由于我们想要改变TextView的不透明度，因此这里传入"alpha"。
         * 后面的参数就是不固定长度了，想要完成什么样的动画就传入什么值，
         * 这里传入的值就表示将TextView从常规变换成全透明，再从全透明变换成常规。
         * 之后调用setDuration()方法来设置动画的时长，然后调用start()方法启动动画
         */

        ObjectAnimator animator = ObjectAnimator.ofFloat(img7, "alpha", 1.0f, 0f, 1.0f);
        animator.setDuration(2000);
        animator.setRepeatCount(10);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.start();
    }

    private static final String TAG = "DongHuaDemoActivity";


    private void valueAnimator() {

        /**
         * ofFloat()方法当中是可以传入任意多个参数的，因此我们还可以构建出更加复杂的动画逻辑，
         * 比如说将一个值在5秒内从0过渡到5，再过渡到3，再过渡到10，就可以这样写：
         * ValueAnimator anim = ValueAnimator.ofFloat(0f, 5f, 3f, 10f);
         * 当然也许你并不需要小数位数的动画过渡，可能你只是希望将一个整数值从0平滑地过渡到100，
         * 那么也很简单，只需要调用ValueAnimator的ofInt()方法就可以了
         *
         * ValueAnimator当中最常用的应该就是ofFloat()和ofInt()这两个方法了，另外还有一个ofObject()方法
         *
         * 那么除此之外，我们还可以调用setStartDelay()方法来设置动画延迟播放的时间，
         * 调用setRepeatCount()和setRepeatMode()方法来设置动画循环播放的次数以及循环播放的模式，
         * 循环模式包括RESTART和REVERSE两种，分别表示重新播放和倒序播放的意思。
         */
        ValueAnimator anim = ValueAnimator.ofFloat(0f, 1.0f);
        anim.setDuration(300);
        ///添加动画监听，在动画执行的时候不断进行回调。
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentValue = (float) animation.getAnimatedValue();
                Log.e(TAG, "onAnimationUpdate: current value is=== " + currentValue);
            }
        });
        anim.start();
    }

    private void RotateAnimation() {

        ObjectAnimator anim = ObjectAnimator.ofFloat(img6, "rotation", 0f, 360f);
        anim.setDuration(800);
        anim.start();
    }

    private void AlpahAnimation() {
        ObjectAnimator anim = ObjectAnimator.ofFloat(img6, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);
        anim.setRepeatCount(-1);
        anim.setRepeatMode(ObjectAnimator.REVERSE);
        anim.setDuration(2000);
        anim.start();
    }

}
